﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WAU.Models.Framework
{
    /// <summary>
    /// 执行结果
    /// </summary>
    public class ExecResult
    {
        /// <summary>
        ///  执行成功（无返回值）
        /// </summary>
        public static readonly ExecResult ExecSuccess = new ExecResult { Success = true };

        /// <summary>
        /// 执行成功（无返回值，有消息）
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public static ExecResult ExecSuccessMsg(string message = "") 
        {
            return new ExecResult { Success = true, ErrorMsg = message };
        }

        /// <summary>
        /// 执行失败
        /// </summary>
        /// <param name="errorMsg"></param>
        public static ExecResult Error(string errorMsg)
        {
            return new ExecResult { Success = false, ErrorMsg = errorMsg };
        }

        /// <summary>
        /// 是否成功
        /// </summary>
        public bool Success { get; set; }
        /// <summary>
        /// 错误信息
        /// </summary>
        public string ErrorMsg { get; set; }
    }

    /// <summary>
    /// 执行结果
    /// </summary>
    public class ExecResult<T>
    {
        /// <summary>
        /// 执行成功并携带数据
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static ExecResult<T> RetSuccess(T data)
        {
            return new ExecResult<T> { Success = true, Data = data };
        }

        /// <summary>
        /// 执行成功并携带数据
        /// </summary>
        /// <param name="data"></param>
        /// <param name="messsage"></param>
        /// <returns></returns>
        public static ExecResult<T> RetSuccess(T data, string messsage)
        {
            return new ExecResult<T> { Success = true, ErrorMsg = messsage, Data = data };
        }

        /// <summary>
        /// 执行失败
        /// </summary>
        /// <param name="errorMsg"></param>
        public static ExecResult<T> Error(string errorMsg)
        {
            return new ExecResult<T> { Success = false, ErrorMsg = errorMsg };
        }

        /// <summary>
        /// 是否成功
        /// </summary>
        public bool Success { get; set; }
        /// <summary>
        /// 错误信息
        /// </summary>
        public string ErrorMsg { get; set; }
        /// <summary>
        /// 要返回的数据
        /// </summary>
        public T Data { get; set; }
    }

    /// <summary>
    /// 执行包装类（供链式调用以简化每次执行时的判断）
    /// </summary>
    public class ExecWrapper
    {
        /// <summary>
        /// 待执行项
        /// </summary>
        private List<Func<ExecResult>> funcs = new List<Func<ExecResult>>();

        /// <summary>
        /// 添加执行项
        /// </summary>
        /// <param name="func"></param>
        public void Add(Func<ExecResult> func)
        {
            funcs.Add(func);
        }

        /// <summary>
        /// 执行
        /// </summary>
        /// <returns></returns>
        public ExecResult Exec()
        {
            if (funcs.Count == 0)
            {
                return new ExecResult { Success = false, ErrorMsg = "没有待执行项" };
            }

            ExecResult finalResult;

            foreach (var func in funcs)
            {
                finalResult = func.Invoke();
                if (!finalResult.Success)
                {
                    return finalResult;
                }
            }

            return ExecResult.ExecSuccess;
        }
    }
}
